package com.gy.boot.controller;

import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.thread.ThreadUtil;
import com.gy.boot.common.aspect.Permissions;
import com.gy.boot.common.dao.IDao;
import com.gy.boot.common.dao.IEntity;
import com.gy.boot.common.dao.IPage;
import com.gy.boot.common.dao.IPageHelp;
import com.gy.boot.common.dao.SqlID;
import com.gy.boot.common.socket.WebSocketServer;
import com.gy.boot.common.util.R;
import com.gy.boot.common.util.UploadUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("test")
public class TestController extends BaseController {

    @Autowired
    IDao<Map<String,Object>> iDao;

    @Autowired
    private WebSocketServer webSocketServer;

    @RequestMapping()
    public Object index() {
        return "<h1>欢迎 访问 我们的 项目 boot-demo </h1>";
    }


    @RequestMapping("list")
    public Object testList() {
//        IPageHelp.start(1, 5);
        IEntity params = IEntity.create(2).set("user_id", 1).set("test_id", 1)
                .set("listId", new int[]{1, 1, 2, 2, 3, 3});
        List<Map<String, Object>> list = iDao.list(SqlID.User_listUserEntity, params);
        System.out.println(list);
        return list;
    }

    /**
     * 系统指定发送消息
     * */
    @GetMapping("sendSocketMsg")
    public Object sendSocketMsg(String id, String msg) {
        webSocketServer.sendMsg(id, msg);
        return R.ok();
    }

    /**
     * 发送广播
     * */
    @GetMapping("sendRadio")
    public Object sendRadio(String msg) {
        webSocketServer.sendRadio(msg);
        return R.ok();
    }


    private static final ThreadLocal<String> T_L = new ThreadLocal<>();

    @GetMapping("TL")
    public Object testTL(String tl) throws InterruptedException {
        System.out.println(tl);
        T_L.set(tl);

        Thread.sleep(10000);

        String tl2 = T_L.get();
        System.out.println(tl2);

        return tl2;
    }


    @GetMapping("page")
    public Object testPage() {
//        IPageHelp.start(1, 10);
        IPage page1 = IPageHelp.getLocalPage();
        System.out.println("page1: " + page1);
        return page1;
    }

    @GetMapping("map")
    public R testMap(@RequestParam Map<String, Object> params) {
        return R.ok().setAll(params);
    }

    @PostMapping("map")
    public R testMap2(@RequestParam Map<String, Object> params) {
        return R.ok().setAll(params);
    }

    @Permissions
    @GetMapping("permissions")
    public R permissions() {
        return R.ok();
    }

    @Permissions()
    @GetMapping("permissions1")
    public R permissions1() {
        return R.ok();
    }

    @Permissions()
    @GetMapping("permissions2")
    public R permissions2() {
        return R.ok();
    }

    @GetMapping("listLog")
    public Object listLog() {
        return IDao.listS(SqlID.SysLog_listLog, null);
    }


    @GetMapping("path")
    public R file(HttpServletRequest request) {
        String home = System.getProperty("user.home");
        System.out.println(home);

        String dir = System.getProperty("user.dir");
        System.out.println(dir);

        R ok = R.ok();
        ok.put("home", home);
        ok.put("dir", dir);
        return ok;
    }

    @GetMapping("logs")
    public R logs() {
        String rootDir = System.getProperty("user.dir");
        String logFile = rootDir + "/logs/";

        List<String> listLogPath = FileUtil.listFileNames(logFile);
        ListUtil.reverse(listLogPath);
        System.out.println(listLogPath);

        R ok = R.ok();
        ok.put("listLogPath", listLogPath);

        return ok;
    }

    @GetMapping("download")
    public void download(HttpServletResponse response) throws IOException {
        String fileUrl = "/files/test2.txt";
        String fileName = "测试";
        UploadUtil.download(fileUrl, fileName, response);
    }



    @GetMapping("intern")
    public void intern() throws InterruptedException {
        long id = 1;
        System.out.println("请求==");
        synchronized (getSynchronizedKey(id)) {
            System.out.println("开始");
            Thread.sleep(3000);
            System.out.println("结束");
        }
    }


    private static final String intern_ = "intern_";
    private static String getSynchronizedKey(long id) {
        return (intern_+id).intern();
    }




    @GetMapping("getPath")
    public String getPath(HttpServletRequest request) {
        return UploadUtil.BASE_PATH;
    }


    @GetMapping("sync")
    public synchronized Object sync() throws InterruptedException {
        Thread.sleep(10000);
        return DateUtil.date().toString();
    }

    @GetMapping("testTh")
    public Object testTh() {
        logger.info("============= 1 =========");
        ThreadUtil.execute(() -> {
            testTh2();
        });
        logger.info("============= 2 =========");
        return R.ok();
    }

    private void testTh2() {
        try {
            Thread.sleep(10000);
            logger.info("============= 3 =========");
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

}
